using System;
using System.Configuration;
using System.Collections;
using System.Xml;

namespace Send2Sharepoint.Common
{
    static class CustomConfigurationSettings
    {
        public static AppSettingsReader AppSettings(string configFile)
        {
            return new AppSettingsReader(configFile);
        }
    }

    public class AppSettingsReader : IDictionary
    {
        private IDictionary _appSettingsDictionary;

        public AppSettingsReader(string configFile)
        {
            _configFile = configFile;
            LoadConfig();
        }

        public AppSettingsReader()
        {
        }

        private void LoadConfig()
        {
            if (_appSettingsDictionary == null)
            {
                try
                {
                    var configXml = new XmlDocument();
                    var configXmlReader = new XmlTextReader(_configFile);
                    configXml.Load(configXmlReader);
                    configXmlReader.Close();
                    var configNodes = configXml.GetElementsByTagName("appSettings");
                    foreach (XmlNode configNode in configNodes)
                    {
                        if (configNode.LocalName != "appSettings") continue;
                        var handler = new DictionarySectionHandler();
                        _appSettingsDictionary = (IDictionary) handler.Create(null, null, configNode);
                    }
                }
                catch
                {
                }
            }
        }

        private string _configFile;

        public string ConfigFile
        {
            get
            {
                return (_configFile);
            }
            set
            {
                _configFile = value;
                LoadConfig();
            }
        }

        public virtual object this[object key]
        {
            get
            {
                string value = null;
                if (_appSettingsDictionary != null)
                {
                    value = _appSettingsDictionary[key] as string;
                }
                return value;
            }
            set
            {

            }
        }

        public void CopyTo(Array array, int index)
        {
            _appSettingsDictionary.CopyTo(array, index);
        }

        public int Count
        {
            get
            {
                return _appSettingsDictionary.Count;
            }
        }

        public bool IsSynchronized
        {
            get
            {
                return _appSettingsDictionary.IsSynchronized;
            }
        }

        public object SyncRoot
        {
            get
            {
                return _appSettingsDictionary.SyncRoot;
            }
        }

        public void Add(object key, object value)
        {
            _appSettingsDictionary.Add(key, value);
        }

        public void Clear()
        {
            _appSettingsDictionary.Clear();
        }

        public bool Contains(object key)
        {
            return _appSettingsDictionary.Contains(key);
        }
        public virtual IDictionaryEnumerator GetEnumerator()
        {
            return null;
        }
        IEnumerator IEnumerable.GetEnumerator()
        {
            return null;
        }
        public bool IsFixedSize
        {
            get
            {
                return _appSettingsDictionary.IsFixedSize;
            }
        }
        public bool IsReadOnly
        {
            get
            {
                return _appSettingsDictionary.IsReadOnly;
            }
        }
        public ICollection Keys
        {
            get
            {
                return _appSettingsDictionary.Keys;
            }
        }
        public ICollection Values
        {
            get
            {
                return _appSettingsDictionary.Values;
            }
        }

        public void Remove(object key)
        {
            _appSettingsDictionary.Remove(key);
        }

    }
}
